package com.fyzk.activity.lottery.basketball;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.tyjc.lottery.R;
import com.fyzk.model.JCLQMatchData;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by miao on 2017/7/22.
 */

public class SFC_Sel_Pop extends PopupWindow {


    //顶部
    private TextView zhu, ke, jlsfc_teamtime;
    //底部
    private TextView cancel, sure;
    protected JCLQMatchData data;

    protected Context context;

    private ListView sfc_team_awaywin, sfc_team_homewin;


    public SFC_Sel_Pop(Context context, int width, int height, JCLQMatchData data) {
        super(width, height);
        this.data = data;
        this.context = context;
        View view = View.inflate(context, R.layout.jl_sfc_dialog, null);
        setContentView(view);
        init();
        info(view);
        setOutsideTouchable(true);
    }

    /**
     * 计算 从投注区返回结果
     *
     * @param infor
     * @return
     */
    protected String getResult(JCInfo infor) {
        String str = "";
        int clickNum = 0;
        for (int i = 0; i < infor.getTouZhuInfor().size(); i++) {
            if (infor.getTouZhuInfor().get(i).isSelect()) {
                str += infor.getTouZhuInfor().get(i).getBiFen() + ",";
                clickNum++;
            }
        }
        infor.setOnclikNum(clickNum);
        if (!str.equals("")) {
            str = str.substring(0, str.length() - 1);
        }
        return str;
    }


    private void info(View view) {
        zhu = view.findViewById(R.id.jlsfc_dialog_home);
        ke = view.findViewById(R.id.jlsfc_dialog_away);
        jlsfc_teamtime = view.findViewById(R.id.jlsfc_teamtime);
        cancel = view.findViewById(R.id.zcbf_dialog_btm_cancel);
        sure = view.findViewById(R.id.zcbf_dialog_btm_sure);
        zhu.setText(data.getHomeTeam() + "[主]");
        ke.setText(data.getAwayTeam() + "[客]");
        String endData = new SimpleDateFormat("HH:mm").format(data.getEndSaleTime());
        jlsfc_teamtime.setText(endData);
        sfc_team_awaywin = view.findViewById(R.id.sfc_team_awaywin);
        sfc_team_homewin = view.findViewById(R.id.sfc_team_homewin);
        cancel.setOnClickListener(new ButtononClick());
        sure.setOnClickListener(new ButtononClick());


        sfc_team_homewin.setAdapter(new JLSFC_DLG_GR_adapter(list_sheng));
        sfc_team_awaywin.setAdapter(new JLSFC_DLG_GR_adapter(list_fu));
    }

    /**
     * 单击事件
     *
     * @author Administrator
     */
    class ButtononClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.zcbf_dialog_btm_cancel:
                    SFC_Sel_Pop.this.dismiss();
                    break;
                case R.id.zcbf_dialog_btm_sure:
                    sureClick();
                    SFC_Sel_Pop.this.dismiss();
                    break;
                default:
                    break;
            }
        }
    }

    // 中间
    private List<JCLQMatchData.SPInfo> list_sheng = new ArrayList<>();
    private List<JCLQMatchData.SPInfo> list_fu = new ArrayList<>();
    private String[] keyArray = null;

    protected void init() {
        keyArray = new String[]{data.SFC_01, data.SFC_02,
                data.SFC_03, data.SFC_04, data.SFC_05,
                data.SFC_06, data.SFC_11, data.SFC_12,
                data.SFC_13, data.SFC_14, data.SFC_15,
                data.SFC_16};
        list_sheng.clear();
        list_fu.clear();
        for (int i = 0; i < keyArray.length; i++) {
            try {
                if (i <= 5) {
                    list_sheng.add((data.splInfos.get(keyArray[i])).clone());
                } else {
                    list_fu.add((data.splInfos.get(keyArray[i])).clone());
                }
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
        }
    }


    protected JCLQMatchData getResult() {
        return data;
    }


    public void sureClick() {
        List<JCLQMatchData.SPInfo> list_all = new ArrayList<>();
        list_all.addAll(list_sheng);
        list_all.addAll(list_fu);
        int clickNum = 0;
        StringBuffer sb = new StringBuffer();
        for (JCLQMatchData.SPInfo info : list_all) {
            int index = list_all.indexOf(info);
            data.splInfos.get(keyArray[index]).isSel = info.isSel;
            if (info.isSel) {
                clickNum++;
                sb.append(info.str);
                sb.append(",");
            }
        }
        data.clickNum = (clickNum);
    }

    /**
     * 适配器
     *
     * @author Administrator
     */
    class JLSFC_DLG_GR_adapter extends BaseAdapter {

        public List<JCLQMatchData.SPInfo> list = new ArrayList<JCLQMatchData.SPInfo>();
        private int count = 0;

        public JLSFC_DLG_GR_adapter(List<JCLQMatchData.SPInfo> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View view, ViewGroup parent) {
            view = LayoutInflater.from(context).inflate(
                    R.layout.jl_sfc_item, null);
            final JLSFC_DLG_GR_adapter.ViewHolder holder = new JLSFC_DLG_GR_adapter.ViewHolder();

            holder.ll1 = view.findViewById(R.id.jlsfc_item);
            holder.tv1 = view.findViewById(R.id.jlsfc_item_descrption);
            holder.tv2 = view.findViewById(R.id.jlsfc_item_sp);

            view.setTag(holder);

            holder.tv1.setText(list.get(position).str);
            holder.tv2.setText(list.get(position).spValue);

            if (!list.get(position).isSel) {
                holder.tv1.setTextColor(context.getResources().getColor(R.color.deep_black));
                holder.tv2.setTextColor(context.getResources().getColor(R.color.deep_black));
                holder.ll1.setBackgroundColor(context.getResources().getColor(R.color.white));
            } else {
                holder.tv1.setTextColor(Color.WHITE);
                holder.tv2.setTextColor(Color.WHITE);
                holder.ll1.setBackgroundColor(context.getResources().getColor(R.color.red));
            }

            holder.ll1.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    boolean isSel = !list.get(position).isSel;
                    list.get(position).isSel = (isSel);
                    if (!list.get(position).isSel) {
                        holder.tv1.setTextColor(Color.BLACK);
                        holder.tv2.setTextColor(Color.BLACK);
                        holder.ll1.setBackgroundResource(R.drawable.zcbf_gr_n);
                        count--;
                    } else {
                        holder.tv1.setTextColor(Color.RED);
                        holder.tv2.setTextColor(Color.RED);
                        holder.ll1.setBackgroundResource(R.drawable.zcbf_gr_c);
                        count++;
                    }
                    JLSFC_DLG_GR_adapter.this.notifyDataSetChanged();
                }
            });

            return view;
        }

        public List<JCLQMatchData.SPInfo> getList() {
            return list;
        }

        public void setList(List<JCLQMatchData.SPInfo> list) {
            this.list = list;
            this.notifyDataSetChanged();
        }

        @Override
        public void notifyDataSetChanged() {
            count = 0;
            super.notifyDataSetChanged();
        }

        class ViewHolder {
            LinearLayout ll1;
            TextView tv1;
            TextView tv2;
        }

    }


}
